/*
 * @Author: zhangshu
 * @email: shiliang.zhang@tuya.com
 * @LastEditors: Please set LastEditors
 * @file name: app_common.h
 * @Description: 
 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
 * @Company: http://www.tuya.com
 * @Date: 2022-02-08
 * @LastEditTime: 2022-02-08
 */
#ifndef __APP_COMMON_H__
#define __APP_COMMON_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include "tuya_zigbee_stack.h"
#include "tuya_cloud_types.h"
#include "tuya_zigbee_attribute.h"

#include "tal_data_send.h"
#include "tal_log.h"



#define IAS_ZONE_ENDPOINT                         1
#define IAS_ZONE_TYPE                             0x8003
#define DEFAULT_TX_POWER                          11
#define BEACON_SEND_INTERVAL_200MS                200  //200ms 
#define DELAY_MS                                  200
#define ZIGBEE_RESET_TIME_MS                      3*1000
#define ZIGBEE_JOIN_TIMEOUT_MS                    180*1000

#if defined(EMBER_AF_PRINT_ENABLE) && defined(TUYA_RUNTIME_DBG)
#include "tkl_uart.h"
#define tal_main_debug(...) tkl_dbg_printf(__VA_ARGS__)
#else
#define tal_main_debug(...) tkl_dbg_printf(__VA_ARGS__)
#endif

#ifndef OPRT_OK
#define OPRT_OK 0
#endif
#ifndef OPRT_COM_ERROR
#define OPRT_COM_ERROR 1
#endif
#ifndef OPRT_INVALID_PARM
#define OPRT_INVALID_PARM 2
#endif

#define MANUFACTURER_CODE                        0x1002

#define INVALID_UINT16_DATA                      0xFFFF
#define INVALID_UINT8_DATA                       0xFF

/******************TUYA PRIVITE ATTRIBUTE************************/
//cluster CLUSTER_TUYA_SENSOR_CLUSTER_ID 0xE002
#define TUYA_SENSOR_HUMAN_MOTION_STATE_ATTRIBUTE_ID          0xE000
#define TUYA_SENSOR_PRESENCE_TIME_ATTRIBUTE_ID               0xE001
#define TUYA_SENSOR_MOTION_FAR_DETECTION_ATTRUBTE_ID         0xE002
#define TUYA_SENSOR_MOTIONLESS_FAR_DETECTION_ATTRIBUTE_ID    0xE003
#define TUYA_SENSOR_MOTION_SENSITIVITY_ATTRIBUTE_ID          0xE004
#define TUYA_SENSOR_MOTIONLESS_SENSITIVITY_ATTRIBUTE_ID      0xE005
#define TUYA_SENSOR_MOTION_AMPLIT_ATTRIBUTE_ID               0xE006
#define TUYA_SENSOR_MOTIONLESS_AMPLIT_ATTRIBUTE_ID           0xE007
#define TUYA_SENSOR_COLOR_TEMPERATURE_ATTRIBUTE_ID           0xE008
#define TUYA_SENSOR_LED_STATE_ATTRIBUTE_ID                   0xE009
#define TUYA_SENSOR_CURRENT_DISTANCE_ATTRIBUTE_ID            0xE00A
#define TUYA_SENSOR_ALL_FAR_DETECTION_ATTRIBUTE_ID           0xE00B
#define TUYA_SENSOR_SETTING_ATTRIBUTE_ID                     0xE00C

#define HIGH_LOW_TO_INT(high, low)  (((UINT16_T)(((UINT16_T)(high)) << 8)) + ((UINT16_T)((low)&0xFF)))

// attribute size 
static const UINT8_T attribute_sizes[] =
{
    #include "attribute-size.h"
};

typedef enum {
  ZIGBEE_CMD_SINGLE = 0,
  ZIGBEE_CMD_GROUP
} ZIGBEE_CMD_E;

typedef enum {
    ZCL_ID_POWER_SOURCE = 100,
    ZCL_ID_ZONE_ENROLL_REQUEST,
    ZCL_ID_ZONE_STATUS_CHANGE_NOTIFICATION,
    ZCL_ID_TEMPERATURE,
    ZCL_ID_TEMPERATURE_MIN,
    ZCL_ID_TEMPERATURE_MAX,
    ZCL_ID_HUMIDITY,
    ZCL_ID_HUMIDITY_MIN,
    ZCL_ID_HUMIDITY_MAX,
    ZCL_ID_ILLUMINANCE,
    ZCL_ID_ILLUMINANCE_MIN,
    ZCL_ID_ILLUMINANCE_MAX,
    ZCL_ID_HUMAN_MOTION_STATE,
    ZCL_ID_PRESENCE_DELAY,
    ZCL_ID_MOTION_FAR_DETECTION,
    ZCL_ID_MOTIONLESS_FAR_DETECTION,
    ZCL_ID_MOTION_SENSITIVITY,
    ZCL_ID_MOTIONLESS_SENSITIVITY,
    ZCL_ID_MOTION_AMPLIT,
    ZCL_ID_MOTIONLESS_AMPLIT,
    ZCL_ID_COLOR_TEMPERATURE,
    ZCL_ID_LED_STATE,
    ZCL_ID_CURRENT_DISTANCE,
    ZCL_ID_MILLIMETER_WAVE_DATA,
    ZCL_ID_TUYA_SENSOR_ALL_A,
    ZCL_ID_TUYA_SENSOR_ALL_B,
    ZCL_ID_TUYA_MCU_OTA_VERSION,
    ZCL_ID_PERCENTAGE,
}ZCL_ID_E;

typedef enum {
  POWER_SOURCE_BATTERY = 3,
  POWER_SOURCE_DC      = 4,
} POWER_SOURCE_E;

/**
 * @brief read attribute data
 * NOTE: only for server clusters attributes
 *
 * @param[in] endpoint: device endpoint id
 * @param[in] cluster_id: cluster id
 * @param[in] attribute_id: attribute id
 * @param[in] data: point to attribute data to be read
 * @param[in] length: length of attribute data
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET read_sensor_attribute(UINT8_T endpoint, 
                                          UINT16_T cluster_id, 
                                          UINT16_T attribute_id, 
                                          VOID_T* data, 
                                          UINT8_T length);

/**
 * @brief report tuya sensor attribute
 * NOTE: only for server clusters attributes
 *
 * @param[in] endpoint: device endpoint id
 * @param[in] cluster_id: cluster id
 * @param[in] attribute_id: attribute id
 * @param[in] attribute_type: attribute type, refer to ZG_ATTR_TYPE_E
 * @param[in] delay_ms: delay send time in ms
 * @return none
 */
VOID_T report_sensor_attribute(UINT8_T endpoint,
                                        UINT16_T cluster_id, 
                                        UINT16_T attribute_id, 
                                        ZG_ATTR_TYPE_E attribute_type, 
                                        UINT16_T delay_ms);

/**
 * @brief write sensor attribute
 * NOTE: only for server clusters attributes
 *
 * @param[in] endpoint: device endpoint id
 * @param[in] cluster_id: cluster id
 * @param[in] attribute_id: attribute id
 * @param[in] data: point to attribute data to be write
 * @param[in] length: length of attribute data
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET write_sensor_attribute(UINT8_T endpoint,
                                           UINT16_T cluster_id,
                                           UINT16_T attribute_id, 
                                           VOID_T* data, 
                                           ZG_ATTR_TYPE_E attribute_type);

/**
 * @brief put attribute paylad into report send buffer
 *
 * @param[out] send_data: send data, please refer to TAL_ZG_SEND_DATA_T
 * @param[in] attribute_num: attribute number
 * @param[in] attribute_id: attribute id
 * @param[in] attribute_type: attribute type, refer to ZG_ATTR_TYPE_E
 * @param[in] data: point to attribute data to be read
 * @return None
 */
VOID_T put_data_into_send_buffer(TAL_ZG_SEND_DATA_T *send_data,
                                           UINT8_T attribute_num,
                                           UINT16_T attribute_id,
                                           ZG_ATTR_TYPE_E attribute_type,
                                           UINT8_T *data);

/**
 * @description: read app version
 * @param[in] none
 * @return: app version id
 */
UINT8_T read_app_version(VOID_T);

/**
 * @description: write power source
 * @param[in] source: power source, please refer to POWER_SOURCE_E
 * @return: none
 */
VOID_T write_power_source(POWER_SOURCE_E source);



extern VOID_T tkl_dbg_printf(const CHAR_T *formatString, ...);

#include "tkl_uart.h"
#include "tlprintf.h"
#define USER_PR_DEBUG(...)     tkl_dbg_printf(__VA_ARGS__)

#ifdef __cplusplus
  extern "C"
  {
#endif
#endif
